
var configureAttr = 'options';
var optionsSeparator = ',';
var modalWindowID = 'modal-window';
var backgroundModalID = 'background-modal-window';
var classModalLink = '.modal-link';
var modalWithoutBackgroundID = 'modal-window-without-back';

$(classModalLink).bind('click',function(event){
	event.preventDefault();
	var urlContent = $(this).attr('href');
	
	var options = $(this).attr(configureAttr);
	var parameters = options.split(optionsSeparator);
	
	var width = parameters[0];
	var height = parameters[1];
	var top = parameters[2];
	var left = parameters[3];

	if(parameters[4] == '1' || parameters[5]== '0'){
		var chordX = event.pageX;
		var chordY = event.pageY+10;
		var top = chordY;
		var left = chordX;
	}
	//si es con fondo o sin fondo
	var withBackground = parameters[5];
	
	showModalWindow(urlContent,width,height,top,left,withBackground);
});


function showModalWindow(urlContent,width,height,top,left,withBackground){
	if(withBackground == '1'){
		var modalWindow = $('#'+modalWindowID);
	}else{
		var modalWindow = $('#'+modalWithoutBackgroundID);
	}
	
	var backgroundModal = $('#'+backgroundModalID);
	modalWindow.css({
		'display':'block',
		'width':width+'px',
		'height':height+'px',
		'top':top+'px',
		'left':left+'px',
	});
	
	if(withBackground == '1'){
		backgroundModal.css({
			'display':'block',
			'opacity':'1'
		});
		backgroundModal.click(function(){
			hideModal($(this),modalWindow);
		});
	}else{
		modalWindow.click(function(){
			hideModal(backgroundModal,$(this));
		});
	}
	
	modalWindow.click(function(){
		return false;
	});
	modalWindow.delay(10).animate(
		{opacity:1},300
	);
	chargeContentModal(urlContent,modalWindow);
}
function chargeContentModal(urlContent,modalWindow){
	var url = urlContent+suffixPage;
	modalWindow.load(url);
	//modalWindow.html(url);
}

function hideModal(backgroundModal,modalWindow){
	modalWindow.animate(
		{opacity:0},300
	);
	backgroundModal.delay(300).animate(
		{opacity:0},
		300,function(){
			backgroundModal.delay(200).hide();
			modalWindow.delay(200).hide();
			modalWindow.html('');
		}
	);
}